<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>动态文字弹幕</title>
    <!-- 引入Tailwind CSS -->
    <script src="https://cdn.tailwindcss.com"></script>
    <!-- 引入Font Awesome -->
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
    
    <!-- 配置Tailwind自定义颜色 -->
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#3B82F6',
                        secondary: '#EC4899',
                        dark: '#1E293B'
                    }
                }
            }
        }
    </script>
    
    <style type="text/tailwindcss">
        @layer utilities {
            .barrage-item {
                position: absolute;
                white-space: nowrap;
                animation: barrageMove linear forwards;
            }
            .btn-hover {
                @apply transition-all duration-300 hover:shadow-lg transform hover:-translate-y-0.5;
            }
        }
    </style>
    
    <style>
        @keyframes barrageMove {
            from {
                transform: translateX(100vw);
            }
            to {
                transform: translateX(-100%);
            }
        }
    </style>
</head>
<body class="bg-gray-100 min-h-screen font-sans">
    <div class="container mx-auto px-4 py-8 max-w-5xl">
        <!-- 页面标题 -->
        <header class="text-center mb-8">
            <h1 class="text-[clamp(1.8rem,4vw,2.5rem)] font-bold text-dark mb-2">
                <i class="fa fa-commenting-o text-primary mr-2"></i>动态文字弹幕
            </h1>
            <p class="text-gray-600">发送你的弹幕，让它在屏幕上飞一会儿</p>
        </header>
        
        <!-- 弹幕显示区域 -->
        <div id="barrageContainer" class="relative w-full h-[50vh] bg-dark rounded-xl overflow-hidden mb-6 shadow-lg">
            <!-- 示例背景图 -->
            <div class="absolute inset-0 opacity-20">
                <img src="https://picsum.photos/1200/600" alt="弹幕背景图" class="w-full h-full object-cover">
            </div>
            
            <!-- 弹幕会通过JS动态添加到这里 -->
        </div>
        
        <!-- 弹幕发送区域 -->
        <div class="bg-white p-5 rounded-xl shadow-md">
            <h2 class="text-lg font-semibold text-gray-800 mb-4 flex items-center">
                <i class="fa fa-paper-plane text-primary mr-2"></i>发送弹幕
            </h2>
            <form id="barrageForm" class="flex flex-col sm:flex-row gap-3">
                <input 
                    type="text" 
                    id="barrageText" 
                    placeholder="输入弹幕内容..." 
                    required
                    class="flex-grow px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary/50 focus:border-primary outline-none transition-all"
                >
                
                <div class="flex gap-3 items-center">
                    <label class="text-gray-700 whitespace-nowrap">颜色:</label>
                    <div class="flex gap-2">
                        <button type="button" data-color="#FFFFFF" class="w-6 h-6 rounded-full bg-white border border-gray-300 color-option active"></button>
                        <button type="button" data-color="#FF4500" class="w-6 h-6 rounded-full bg-[#FF4500] color-option"></button>
                        <button type="button" data-color="#32CD32" class="w-6 h-6 rounded-full bg-[#32CD32] color-option"></button>
                        <button type="button" data-color="#FFFF00" class="w-6 h-6 rounded-full bg-[#FFFF00] color-option"></button>
                        <button type="button" data-color="#FF69B4" class="w-6 h-6 rounded-full bg-[#FF69B4] color-option"></button>
                    </div>
                </div>
                
                <button 
                    type="submit" 
                    class="bg-primary text-white px-5 py-2 rounded-lg font-medium btn-hover flex items-center justify-center"
                >
                    <i class="fa fa-rocket mr-2"></i>发送
                </button>
            </form>
            
            <div class="mt-4 text-sm text-gray-500">
                <p><i class="fa fa-info-circle mr-1"></i> 提示: 输入文字后点击发送，即可看到你的弹幕在屏幕上滚动</p>
            </div>
        </div>
        
        <!-- 页脚 -->
        <footer class="mt-10 text-center text-gray-500 text-sm">
            <p>© 2023 动态弹幕演示 | 简单有趣的文字互动效果</p>
        </footer>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const barrageForm = document.getElementById('barrageForm');
            const barrageContainer = document.getElementById('barrageContainer');
            const barrageText = document.getElementById('barrageText');
            const colorOptions = document.querySelectorAll('.color-option');
            let selectedColor = '#FFFFFF'; // 默认白色
            
            // 颜色选择
            colorOptions.forEach(option => {
                option.addEventListener('click', function() {
                    // 移除其他选项的active状态
                    colorOptions.forEach(opt => opt.classList.remove('active', 'ring-2', 'ring-primary'));
                    // 添加当前选项的active状态
                    this.classList.add('active', 'ring-2', 'ring-primary');
                    // 保存选中的颜色
                    selectedColor = this.getAttribute('data-color');
                });
            });
            
            // 表单提交事件
            barrageForm.addEventListener('submit', function(e) {
                e.preventDefault();
                const text = barrageText.value.trim();
                
                if (text) {
                    addBarrage(text, selectedColor);
                    // 清空输入框
                    barrageText.value = '';
                    // 聚焦输入框
                    barrageText.focus();
                }
            });
            
            // 添加弹幕函数
            function addBarrage(text, color) {
                const containerHeight = barrageContainer.offsetHeight;
                const containerWidth = barrageContainer.offsetWidth;
                
                // 创建弹幕元素
                const barrage = document.createElement('div');
                barrage.className = 'barrage-item px-3 py-1 rounded-md';
                barrage.style.color = color;
                barrage.style.fontSize = `${Math.floor(Math.random() * 4) + 14}px`; // 14-17px 随机大小
                barrage.style.top = `${Math.floor(Math.random() * (containerHeight - 30))}px`; // 随机垂直位置
                barrage.textContent = text;
                
                // 计算动画时长 (文字越长，速度越快)
                const duration = Math.max(8 - text.length / 5, 4); // 4-8秒
                barrage.style.animationDuration = `${duration}s`;
                
                // 添加到容器
                barrageContainer.appendChild(barrage);
                
                // 动画结束后移除元素
                setTimeout(() => {
                    // 添加淡出效果
                    barrage.style.opacity = '0';
                    setTimeout(() => {
                        barrage.remove();
                    }, 500);
                }, duration * 1000);
            }
            
            // 预设一些示例弹幕
            const exampleBarrages = [
                "这个弹幕效果好棒啊！",
                "Hello World!",
                "我在学习前端开发",
                "大家好呀~",
                "这个页面看起来不错",
                "666666",
                "前端真有趣",
                "Tailwind CSS很好用"
            ];
            
            const exampleColors = ["#FFFFFF", "#FF4500", "#32CD32", "#FFFF00", "#FF69B4"];
            
            // 定时发送示例弹幕
            function addRandomExampleBarrage() {
                if (Math.random() > 0.7) { // 70%概率发送
                    const randomText = exampleBarrages[Math.floor(Math.random() * exampleBarrages.length)];
                    const randomColor = exampleColors[Math.floor(Math.random() * exampleColors.length)];
                    addBarrage(randomText, randomColor);
                }
                
                // 2-5秒后再次尝试发送
                setTimeout(addRandomExampleBarrage, Math.floor(Math.random() * 3000) + 2000);
            }
            
            // 开始发送示例弹幕
            addRandomExampleBarrage();
        });
    </script>
</body>
</html>
    